package com.demo.mapper;


import com.demo.dto.DepartmentDTO;
import com.demo.dto.UserDTO;
import com.demo.entity.SysUser;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysUserMapper {

    //    @Select("select * from sys_user where user_id=#{userId} and disabled=0")
    @Select("select sys_user .*,sys_department.dep_name " +
            "from sys_user left join sys_department on sys_user.dep_id =sys_department .id " +
            "where  user_id =#{userId}")
    SysUser getByUserId(String userId);

    @Select("select * from sys_user where login_name=#{loginName} and disabled=0")
    SysUser getByLoginname(String loginName);

    @Insert("INSERT INTO sys_user " +
            "(user_id, login_name, login_pass, last_login, dep_id, telephone, ding_ding," +
            " create_time, disabled, role_id, true_name)" +
            "VALUES(#{userId}, #{loginName}, #{loginPass}, #{lastLogin}, #{depId},#{telephone}," +
            "#{dingDing}, #{createTime}, #{disabled}, #{roleId}, #{trueName})")
    int insert(SysUser user);




    @Select("<script>" +
            "select sys_user.*, crm_role.role_name as roleName,sd.dep_name\n" +
            "from sys_user\n" +
            "         left join crm_role on sys_user.role_id = crm_role.role_id\n" +
            "        left join sys_department sd on dep_id=sd.id\n" +
            "order by create_time desc" +
            "</script> ")
    List<SysUser> list();

    @Select("<script>" +
            "select sys_user.*, crm_role.role_name as roleName,sd.dep_name\n" +
            "from sys_user\n" +
            "         left join crm_role on sys_user.role_id = crm_role.role_id\n" +
            "        left join sys_department sd on dep_id=sd.id\n" +
            "where sys_user.disabled is not null " +
            "<if test='depId != null and depId!=\"\"'>" +
            " and  dep_id=#{depId} " +
            "</if>" +
            "\n" +
            "order by sys_user.create_time desc" +
            "</script>")
    List<SysUser> listDep(String depId);


    @Select("select dep_name from sys_department where id=#{id}")
    String depName(String id);

    @Update("UPDATE sys_user " +
            "SET dep_id=#{depId}, telephone=#{telephone}," +
            " ding_ding=#{dingDing}," +
            " role_id=#{roleId}, true_name=#{trueName}" +
            " WHERE login_name=#{loginName}")
    int update(SysUser user);

    @Update("UPDATE sys_user set disabled=1 where login_name=#{loginName}")
    int remove(String loginName);

    @Update("UPDATE sys_user SET login_pass=#{loginPass} WHERE login_name=#{loginName}")
    int password(String loginName, String loginPass);

    @Select({"<script>" +
            "select count(*) from sys_user " +
            "<if test='loginName != null and loginName!=\"\"'>" +
            " where login_name=#{loginName}" +
            "</if>" +
            "<if test='telephone != null and telephone!=\"\"'>" +
            " or telephone=#{telephone}" +
            "</if>" +
            "<if test='dingDing != null and dingDing!=\"\"'>" +
            " or ding_ding=#{dingDing}" +
            "</if>" +
            "</script>"}
    )
    int confirmLoginName(String loginName, String telephone, String dingDing);

    @Select("select sys_user .*,sys_department.dep_name from sys_user left join sys_department on sys_user.dep_id =sys_department .id \n" +
            "where  user_id =#{userId}")
    SysUser findFromUserID(String userId);

    @Select("select sys_user.user_id,sys_user.true_name,sys_department.id dep_id," +
            "sys_department.dep_name" +
            " from sys_user inner join sys_department on sys_user.dep_id=sys_department.id" +
            " where sys_user.disabled=0 and sys_department.disabled=0" +
            " order by sys_user.dep_id")
    List<UserDTO> findDepartmentUser();

    @Update("UPDATE sys_user set disabled =0 WHERE user_id =#{userId}")
    int backUser(String userId);

    @Select("SELECT login_pass FROM sys_user where login_name =#{loginName} ")
    String pass(String loginName);

    @Update("update sys_user set role_id=#{roleId} where user_id=#{userId}")
    int roleId(String userId, String roleId);


    @Results(value = {
            @Result(column = "dep_id", property = "depId"),
            @Result(column = "dep_name", property = "depName"),
            @Result(column = "dep_id", property = "userList", javaType = List.class,
                    many = @Many(select = "com.demo.mapper.SysUserMapper.findByDepartment"))
    })
    @Select("select id dep_id,dep_name from sys_department where disabled=0 order by dep_id")
    List<DepartmentDTO> findDepartmentUserList();

    @Select("select * from sys_user where disabled=0 and dep_id=#{depId}")
    List<SysUser> findByDepartment(String depId);

    @Select("select * from sys_user where disabled=0 ")
    List<SysUser> findAllSystemUser();

    @Select("select * from sys_user where user_id=#{userId}")
    SysUser findUserById(String userId);

    @Select("SELECT * FROM sys_user where (role_id ='005') OR (dep_id ='009' AND role_id ='004' ) " +
            "OR (dep_id ='004' AND role_id ='004')\n" +
            "OR (dep_id ='005' AND role_id ='004') OR (dep_id ='001')  AND disabled =0")
    List<SysUser> findAllLeader();

}